package com.tangcco.lockwell.controller.admin;

import com.tangcco.lockwell.base.RestResponse;
import com.tangcco.lockwell.domain.GroupsRelationship;
import com.tangcco.lockwell.service.GroupsRelationshipService;
import com.tangcco.lockwell.viewmodel.admin.groups.GroupsRelationshipUpdateVM;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.tangcco.lockwell.utility.DateToString.dateToString;
/**
 * 群组用户关系控制器
 * @author 李金昭
 */
@RequestMapping("/api/admin/GroupsRelationship")
@RestController("AdminGroupsRelationshipController")
public class GroupsRelationshipController {

    @Resource
    private GroupsRelationshipService groupsRelationshipService;

    //添加
    @RequestMapping("/insert")
    public RestResponse insert(@RequestBody GroupsRelationship groupsRelationship) {
        //添加时间
        groupsRelationship.setUseraddTime(dateToString(new Date()));
        //默认状态正常
        groupsRelationship.setStatus(1);
        groupsRelationshipService.insert(groupsRelationship);
        return RestResponse.ok(groupsRelationship);
    }

    //删除 根据id删除 或者 通过groupId批量删除
    @RequestMapping("/delete")
    public RestResponse delete(@RequestBody GroupsRelationship groupsRelationship) {
        groupsRelationshipService.deleteGroupUserByConditionsSelective(groupsRelationship.getUserId(),groupsRelationship.getGroupId());
        return RestResponse.ok();
    }


    //查询
    @RequestMapping("/select/{groupId}/{userId}")
    public RestResponse select(@PathVariable Integer groupId,@PathVariable Integer userId) {
        GroupsRelationship groupsRelationship = groupsRelationshipService.findGroupUsersByConditions(groupId,userId);
        return RestResponse.ok(groupsRelationship);
    }
    //查询
    @RequestMapping("/findGroupUsersByGroupId/{groupId}")
    public RestResponse findGroupUsersByGroupId(@PathVariable Integer groupId) {
        List<GroupsRelationship> groupsRelationship = groupsRelationshipService.findByGroupIdAndUserType(groupId);
        if(CollectionUtils.isEmpty(groupsRelationship)){
            return RestResponse.ok();
        }
        return RestResponse.ok(groupsRelationship.stream().map(GroupsRelationship::getUserId).collect(Collectors.toList()));
    }

    //修改状态和权限
    @RequestMapping("/updateStatus")
    public RestResponse updateStatus(GroupsRelationshipUpdateVM groupsRelationshipUpdateVM) {
        GroupsRelationship groupsRelationship = null;
        if (groupsRelationshipUpdateVM.getId()==null || groupsRelationshipUpdateVM.getStatus()==null&&groupsRelationshipUpdateVM.getPermission()==null){
            return new RestResponse<>(3, "参数错误");
        }
        int rows = groupsRelationshipService.updateGroup(groupsRelationshipUpdateVM);
        if (rows > 0) {
            groupsRelationship = groupsRelationshipService.selectById(groupsRelationshipUpdateVM.getId());
        }
        return RestResponse.ok(groupsRelationship);
    }


}
